<template>
	<view>
		<view class="search-box">
			<uni-search-bar
				:radius="100"
				placeholder="请输入搜索内容" 
				clearButton="always" 
				cancelButton="none" 
				v-model="searchVal"
				focus="true"
				@input="input"
				/>
		</view>
		<view class="search-box-title" v-if="inputText.length == 0">
			<view class="search-box-top">
				<text>搜索历史</text>
				<uni-icons type="trash-filled" size="20" @click="deleteSearch"></uni-icons>
			</view>
			<view class="search-box-bot">
				<view class="search-box-bot-item" v-for="(item,i) in historyList" :key="i" @click="getSearchItem(item)">
					<text>{{item}}</text>
				</view>
			</view>
		</view>
		<!-- 搜索建议列表 -->
		<view class="search-list">
			<view class="search-item" v-for="(item,i) in searchResults" :key="i" @click="getItems(item)">
				<text class="search-text">{{item.goods_name}}</text>
				<uni-icons type="forward" size="16"></uni-icons>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchVal: '',
				timer: null,
				//搜索的字段
				inputText: '',
				//搜索
				searchResults: [],
				//搜索关键词历史
				historyList: []
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('inputText') || '[]')
		},
		methods:{
			input(res){
				clearTimeout(this.timer)
				
				this.timer = setTimeout(() => {
					this.inputText = res
					this.getSearchList()
				}, 500)
			},
			//搜索商品信息
			async getSearchList() {
			  // 判断关键词是否为空
			  if (this.inputText === '') {
			    this.searchResults = []
			    return
			  }
			  // 发起请求，获取搜索建议列表
			  const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.inputText })
			  if (res.meta.status !== 200) return uni.$showMessage()
			  this.searchResults = res.message
			  // 1. 查询到搜索建议之后，调用 saveSearchHistory() 方法保存搜索关键词
			  this.saveSearchHistory()
			},
			// 2. 保存搜索关键词的方法
			saveSearchHistory(){
				// this.historyList.push(this.inputText)
				// 1. 将 Array 数组转化为 Set 对象
				// const set = new Set(this.historyList)
				// 2. 调用 Set 对象的 delete 方法，移除对应的元素
				const i =  this.historyList.indexOf(this.inputText)
				
				if(i == -1){
					this.historyList.unshift(this.inputText)
					uni.setStorageSync('inputText',JSON.stringify(this.historyList))
				}else{
					this.historyList.splice(i,1)
					this.historyList.unshift(this.inputText)
					uni.setStorageSync('inputText',JSON.stringify(this.historyList))
				}
				
			},
			//点击搜索栏跳转到购物页
			getItems(data){
				uni.navigateTo({
					url:"/subpkg/goods_detail/goods_detail?goods_id="+ data.goods_id
				})
			},
			//删除搜索记录
			deleteSearch(){
				this.historyList = ''
				uni.removeStorageSync('inputText')
			},
			//点击搜索历史跳转
			getSearchItem(item){
				this.input(item)
				this.searchVal = item
			}
		}
	}
</script>

<style lang="scss">
.search-box{
	background-color: red;
}
.search-list{
	padding: 0 5px;
	.search-item{
		border-bottom: 1px solid #efefef;
		align-items: center;
		//均匀分布在一行中
		justify-content: space-between;
		display: flex;
		font-size: 12px;
		padding:13px 0;
		.search-text{
			// 文字不允许换行（单行文本）
			white-space: nowrap;
			// 溢出部分隐藏
			overflow: hidden;
			// 文本溢出后，使用 ... 代替
			text-overflow: ellipsis;
		}
	}
}
.search-box-title{
	.search-box-top{
		display: flex;
		justify-content: space-between;
		border-bottom: 1px solid #efefef;
		line-height: 40px;
		font-size: 12px;
		height: 40px;
		
	}
	.search-box-bot{
		display: flex;
		flex-wrap: wrap-reverse;
		.search-box-bot-item{
			padding: 3px 10px;
			background-color: #efefef;
			margin: 5px 5px;
		}
	}
}
</style>
